package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.CountDownTimer
import android.provider.MediaStore
import android.text.method.PasswordTransformationMethod
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import yja.gzw.app.R
import com.bumptech.glide.Glide
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityZhuceBinding
import yja.gzw.app.viewmodel.Mainviewmodel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.window.Chicun
import java.io.File

class ZhuceActivity :BaseActivity<Mainviewmodel, ActivityZhuceBinding>(R.layout.activity_zhuce){
    var file: File?=null
    var banpath=""
    var panduan="zm"
    var shfzmpath=""
    var sfzfmpath=""
    val chicun= Chicun()
    private var issee=true
    private var issee3=true
    private var issee4=true
    private var countDownTimer: CountDownTimer? = null
    override fun initView() {

        chicun.chicunwai(binding.yanjing2,0,0,15,0)
        chicun.chicun(binding.yanjing2,80,25)
        binding.emilPaw.transformationMethod = PasswordTransformationMethod.getInstance()
        binding.emilPaw3.transformationMethod = PasswordTransformationMethod.getInstance()
        binding.emilPaw4.transformationMethod = PasswordTransformationMethod.getInstance()





        binding.yanjing.setOnClickListener {
            if(issee){
                binding.yanjing.setImageResource(R.mipmap.chakanshu)
                binding.emilPaw.transformationMethod = null // 密码框内容设为可见

                issee=!issee
            }else{
                binding.yanjing.setImageResource(R.mipmap.yanjingguan)
                binding.emilPaw.transformationMethod = PasswordTransformationMethod.getInstance() // 密码框内容设为不可见

                issee=!issee
            }



        }

        binding.yanjing3.setOnClickListener {
            if(issee3){
                binding.yanjing.setImageResource(R.mipmap.chakanshu)
                binding.emilPaw3.transformationMethod = null // 密码框内容设为可见

                issee3=!issee3
            }else{
                binding.yanjing.setImageResource(R.mipmap.yanjingguan)
                binding.emilPaw3.transformationMethod = PasswordTransformationMethod.getInstance() // 密码框内容设为不可见

                issee3=!issee3
            }



        }
        binding.yanjing4.setOnClickListener {
            if(issee4){
                binding.yanjing.setImageResource(R.mipmap.chakanshu)
                binding.emilPaw4.transformationMethod = null // 密码框内容设为可见

                issee4=!issee4
            }else{
                binding.yanjing.setImageResource(R.mipmap.yanjingguan)
                binding.emilPaw4.transformationMethod = PasswordTransformationMethod.getInstance() // 密码框内容设为不可见

                issee4=!issee4
            }



        }




        binding.fanhui.setOnClickListener {
        finish()


    }

        binding.sfzzm.setOnClickListener {

            panduan="zm"


            openGallery()


        }

        binding.sfzbm.setOnClickListener {

            panduan="bm"


            openGallery()

        }
        binding.yanjing2.setOnClickListener {
            val shoujihao=binding.emilName2.text.toString()
            Toast.makeText(this,"获取验证码中请稍后...", Toast.LENGTH_SHORT).show()

            viewmodel.onzhuceyzmwang(mapOf("mobile" to shoujihao ,"event" to "register"))
            binding.yanjing2.isEnabled = false
            countDownTimer = object : CountDownTimer(60000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    binding.yanjing2.text = "${millisUntilFinished / 1000}s"
                }
                override fun onFinish() {
                    binding.yanjing2.isEnabled = true
                    binding.yanjing2.text = "重新获取"
                }
            }.start()



        }
        binding.queding.setOnClickListener {
            val shoujihao=binding.emilName2.text.toString()
            val yanzhengma=binding.emilPaw2.text.toString()
            val mima1=binding.emilPaw.text.toString()
            val mima2=binding.emilPaw3.text.toString()
            val youxiang=binding.emilPaw4.text.toString()
            val xingming=binding.xiangming.text.toString()
            val sfzhaoma=binding.shengfengzheng.text.toString()
            viewmodel.onzhuceqingqiuwang(mapOf("mobile" to shoujihao,"password" to mima1,"confirmpassword" to mima2,"id_card" to sfzhaoma,"username" to xingming,"email" to youxiang,"id_card_z" to shfzmpath,"id_card_b" to sfzfmpath,"code" to yanzhengma))

        }







    }


    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }
    override fun dataObservese() {

        viewmodel.onzhuceqingqiu.observe(this, Observer {
            if(it.code==1){


                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                finish()

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()


            }

        })


        viewmodel.onzhuceyzm.observe(this, Observer {
            Log.e("开始","111")
            if(it.code==1){
                Log.e("开始","222")
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Log.e("开始","333")
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })



        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){
                if(panduan=="zm"){
                    shfzmpath=it.data.url
                    Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.url)
                        .into(binding.sfzzm)


                }else{
                    sfzfmpath=it.data.url
                    Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.url)
                        .into(binding.sfzbm)


                }


                Log.e("上传图片的半路径",it.data.url)


                banpath=it.data.url
                Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()

            }


        })


    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }

}